package com.hand.hap.demo.service.impl;

import com.hand.hap.code.rule.exception.CodeRuleException;
import com.hand.hap.code.rule.service.ISysCodeRuleProcessService;
import com.hand.hap.core.IRequest;
import com.hand.hap.demo.dto.OmOrderHeaders;
import com.hand.hap.demo.dto.OmOrderLines;
import com.hand.hap.demo.mapper.OmOrderHeadersMapper;
import com.hand.hap.demo.service.*;
import com.hand.hap.system.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.List;

/**
 * 订单头serviceImpl
 * @author shuai.wang02@hand-china.com
 * @date  2018/8/25 19:51
 */

@Service
public class OmOrderHeadersServiceImpl extends BaseServiceImpl<OmOrderHeaders> implements IOmOrderHeadersService {

    @Autowired
    IOrgCompanysService orgCompanysService;
    @Autowired
    IOmOrderLinesService omOrderLinesService;
     @Autowired
     IOmOrderHeadersService orderHeadersService;
    @Autowired
    IInvInventoryItemsService invInventoryItemsService;
    @Autowired
    OmOrderHeadersMapper omOrderHeadersMapper;
     @Autowired
    IArCustomersService arCustomersService;

    @Autowired
    ISysCodeRuleProcessService codeRuleProcessService;


    /**
     *
     *  通过编码规则获取订单编号
     * @return 订单编号
     * @author  shuai.wang02@hand-china.com
     * @date  2018/8/24 15:29
     */
    public String getOrdernumCode() throws CodeRuleException {
        return codeRuleProcessService.getRuleCode("HSCS_RULE_ORDER_ORDERNUM");
    }

    @Override
    public List<OmOrderHeaders> selectOrderHeaders(IRequest requestContext, OmOrderHeaders dto, int page, int pageSize) {
        List<OmOrderHeaders> list;

        if (dto.getCompanyId() == null && dto.getInventoryItemId() == null && dto.getCustomerId() == null && dto.getOrderStatus() == null && dto.getOrderNumber() == null) {
            list = mapper.selectAll();

        } else {
            list = selectOrderHeadersByTerms(dto);

        }

        if (list != null && !list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
                String companyName = selectCompanyNameByCompanyId(list.get(i).getCompanyId());
                list.get(i).setCompanyName(companyName);
                String customerName = arCustomersService.selectByCustomerId(list.get(i).getCustomerId());
                String orderTotalPrices = calculationOrderAmount(list.get(i).getHeaderId());
                list.get(i).setOrderTotalPrices(orderTotalPrices);
                list.get(i).setCustomerName(customerName);
            }
        }
        return list;
    }



    @Override
    public List<OmOrderHeaders> selectOrderHeadersByTerms(OmOrderHeaders dto) {
        List<OmOrderHeaders> list;

        if (dto.getCompanyId() == null && dto.getInventoryItemId() == null && dto.getCustomerId() == null && dto.getOrderStatus() == null && dto.getOrderNumber() == null) {
            list = mapper.selectAll();

        } else {
            list = omOrderHeadersMapper.selectOrderHeadersByTerms(dto);

        }
        return  list;
    }


    @Override
    public OmOrderHeaders queryByHeaderId(IRequest requestContext, OmOrderHeaders dto, int page, int pageSize) {
        OmOrderHeaders orderHeaders  = mapper.selectByPrimaryKey(dto);
        String companyName = selectCompanyNameByCompanyId(orderHeaders.getCompanyId());
        orderHeaders.setCompanyName(companyName);
        String customerName = arCustomersService.selectByCustomerId(orderHeaders.getCustomerId());
        String orderTotalPrices = calculationOrderAmount(orderHeaders.getHeaderId());
        orderHeaders.setOrderTotalPrices(orderTotalPrices);
        orderHeaders.setCustomerName(customerName);
        return orderHeaders;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmOrderHeaders> submitHeaderAndLines(IRequest request, List<OmOrderHeaders> list) throws CodeRuleException {
        if (list != null && !list.isEmpty()) {

            for (int i = 0; i < list.size(); i++) {
                switch(list.get(i).getOrderStatus()){
                    case "已提交":
                        list.get(i).setOrderStatus("SUBMITED");
                        break;
                    case "已审批":
                        list.get(i).setOrderStatus("APPROVED");
                        break;
                    case "已拒绝":
                        list.get(i).setOrderStatus("REJECTED");
                        break;
                    default:
                        break;
                }
                OmOrderHeaders orderHeader = list.get(i);
                Long headerId = orderHeader.getHeaderId();
                if (headerId == null||headerId==0) {
                    //获取订单编码并传入
                    orderHeader.setOrderNumber(getOrdernumCode());
                    orderHeadersService.insertSelective(request,orderHeader);
                    headerId=orderHeader.getHeaderId();
                    List<OmOrderLines> orderLinesList = orderHeader.getOrderLinesList();
                    Long companyId = orderHeader.getCompanyId();


                    if (orderLinesList!=null &&!orderLinesList.isEmpty()){
                        for (int j = 0; j < orderLinesList.size(); j++) {
                            orderLinesList.get(j).setCompanyId(companyId);
                            orderLinesList.get(j).setHeaderId(headerId);
                        }
                    }

                    omOrderLinesService.submitLines(request,orderLinesList);
                } else {

                    orderHeadersService.updateByPrimaryKeySelective(request,orderHeader);
                    List<OmOrderLines> orderLinesList = orderHeader.getOrderLinesList();
                    Long companyId = orderHeader.getCompanyId();
                    if (orderLinesList!=null &&!orderLinesList.isEmpty()){
                        for (int j = 0; j < orderLinesList.size(); j++) {

                            if (orderLinesList.get(j).getCompanyId()==null){
                                orderLinesList.get(j).setCompanyId(companyId);
                            }
                            if (orderLinesList.get(j).getHeaderId()==null){
                                orderLinesList.get(j).setHeaderId(headerId);
                            }
                        }
                    }
                    omOrderLinesService.submitLines(request,orderLinesList);
                }
            }
        }

        return list;
    }

    @Override
    public List<OmOrderHeaders> selectExcelData(IRequest requestContext, OmOrderHeaders dto,int page, int pageSize) {

        List<OmOrderHeaders> list;

        if (dto.getCompanyId() == null && dto.getInventoryItemId() == null && dto.getCustomerId() == null && dto.getOrderStatus() == null && dto.getOrderNumber() == null) {
            list = omOrderHeadersMapper.selectExcelAllData();

        } else {
            list = omOrderHeadersMapper.selectExcelData(dto);

        }

        return list;

    }



    public String selectCompanyNameByCompanyId(Long companyId) {
        return orgCompanysService.selectByCompanyId(companyId);
    }


    public String calculationOrderAmount(Long headerId) {
        List<OmOrderLines> orderLinesList = omOrderLinesService.selectByHeaderId(headerId);

        Double orderTotalPrices = 0D;

        if (orderLinesList != null && !orderLinesList.isEmpty()) {
            for (int j = 0; j < orderLinesList.size(); j++) {
                orderTotalPrices = orderTotalPrices + (orderLinesList.get(j).getOrderdQuantity() * orderLinesList.get(j).getUnitSellingPrice());
            }
        }
        DecimalFormat    df   = new DecimalFormat("######0.00");
        return df.format(orderTotalPrices);
    }
}